﻿using LibNiconico.NiconicoApi;
using LibNiconico.NiconicoApi.Lives;
using LibNiconico.NiconicoApi.Lives.Comments;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NicoView.Model.Service.Comments
{
    public sealed class NicoSendComment
    {
        public NicoSendComment()
        {
            Body = "";
            IsAnonymous = false;
            IsOwner = false;
            Color = new NicoCommentColorWhite();
            Position = new NicoCommentCenterPosition();
            Size = new NicoCommentMediumSize();
            ExtendMails = new List<INicoCommentMailComand>();
        }

        public string Body { get; set; }

        public bool IsAnonymous { get; set; }

        public INicoCommentColor Color { get; set; }

        public INicoCommentSize Size { get; set; }

        public bool IsOwner { get;  set; }

        public INicoCommentPosition Position { get; set; }

        public ICollection<INicoCommentMailComand> ExtendMails { get; private set; }

        private void setMails(ICollection<INicoCommentMailComand> mails)
        {
            if (!Color.IsDefault) mails.Add(Color);
            if (!Size.IsDefault) mails.Add(Size);
            if (!Position.IsDefault) mails.Add(Position);
        }

        private void setEnvMails(ICollection<string> mails)
        {
            var commandMails = new List<INicoCommentMailComand>();

            setMails(commandMails);

            convertToMail(commandMails, mails);
        }

        internal NicoLiveSubmitChatComment AsLiveSubmitComment()
        {
            var chat = new NicoLiveSubmitChatComment();

            chat.Body = Body;
            setEnvMails(chat.Mails);
            
            if (IsAnonymous) chat.Mails.Add("184");
            

            return chat;

        }

        internal NicoLiveOwnerChatComment AsLiveOwnerSubmitComment()
        {
            var chat = new NicoLiveOwnerChatComment();
            chat.Body = Body;
            setEnvMails(chat.Mails);
            chat.IsAnonymous = this.IsAnonymous;

            return chat;

        }

        private static void convertToMail(IEnumerable<INicoCommentMailComand> mailComands, ICollection<string> mails)
        {

            foreach (var command in mailComands)
            {
                mails.Add(command.Command);
            }

        }
    }
}
